package com.feicheng.controller.system;

import jakarta.annotation.Resource;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Permission;
import com.feicheng.service.IFSystemService;

@RestController
@RequestMapping("/admin")
public class SystemController {

	@Resource(name = "system-admin-service")
	IFSystemService iFSystemService;

	@GetMapping("/permission/page")
	public ResultInfo getPermissionsByPage(
			String name, String path, Boolean aFlg, String pCode,
            @RequestParam(defaultValue = "1")Integer currentPage,
            @RequestParam(defaultValue = "10")Integer pageSize) {
		return iFSystemService.getPermissions(name, path, aFlg, pCode, currentPage, pageSize);
		
	}
	
	@PostMapping("/permission/info")
	public ResultInfo newPermission(String permission) {
		if (!StringUtils.hasLength(permission)) {
			// 前端没往后端传值
			throw new SystemException("");
		}
		Permission permissionObject = JSONObject.parseObject(permission, Permission.class);
		return iFSystemService.addPermission(permissionObject);
	}

	// 有疑点,传过来的对象permission所有信息是可以改的,无法找到原先的信息
	@PutMapping("/permission/info")
	public ResultInfo updatePermission(String permission) {
		if (!StringUtils.hasLength(permission)) {
			// 前端没往后端传值
			throw new SystemException("");
		}
		Permission permissionObject = JSONObject.parseObject(permission, Permission.class);
		return iFSystemService.updatePermission(permissionObject);
	}

	@DeleteMapping("/permission/info")
	public ResultInfo deletePermission(String code) {
		if (!StringUtils.hasLength(code)) {
			// 前端没往后端传值
			throw new SystemException("");
		}
		return iFSystemService.deletePermission(code);
	}

	@GetMapping("/permission/all")
	public ResultInfo allPermissions(Boolean statu, String pCode) {
		return iFSystemService.allPermissions(statu, pCode);
	}

	@GetMapping("/permission/info")
	public ResultInfo getPermission(String code) {
		return iFSystemService.getPermission(code);
	}
	
	

}
